﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;

namespace MyAssetBundles {
    public class MyVersionOperation {
        public MyAssetBundlesUpdateMode updateMode;
        public string version;
        public long compressSize;
        public long decompressSize;
        public string compressPackage;
        public string decompressPackage
        {
            get {
                return Path.ChangeExtension(compressPackage, ".temp.civ");
            }
        }

        public string savePath
        {
            set {
                compressPackage = MyAssetBundlesUtil.PathCombine(value, Path.GetFileName(compressPackage));
            }
        }
    }

    public class MyAssetBundlesVersions : IEnumerable<MyVersionOperation> {
        protected List<MyVersionOperation> _versionsList = new List<MyVersionOperation>();
        public string _packageDirectory;
        public string _filePath;
        public int Count {
            get {
                return _versionsList.Count;
            }
        }
        public MyAssetBundlesVersions (string packageDirectory)
        {
            _packageDirectory = packageDirectory;
        }

        public void LoadLocalPath (string path)
        {
            _filePath = path;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            if (!File.Exists(path))
                File.Create(path).Close();
            LoadVersions(File.ReadAllText(path));
        }

        public IEnumerator LoadWebPath (string Path)
        {
            Debug.Log("load web path: " + Path);
            WWW w = new WWW(Path);
            yield return w;
            LoadVersions(w.text);
        }

        protected virtual void LoadVersions (string content)
        {
            _versionsList.Clear();
            StringReader sReader = new StringReader(content);
            while(true)
            {
                try
                {
                    string verStr = sReader.ReadLine();
                    if (verStr == null) break;
                    string[] arr = verStr.Split('#');
                    if (arr.Length < 5) continue;
                    MyVersionOperation operation = new MyVersionOperation();
                    operation.version = arr[0];
                    operation.compressPackage = MyAssetBundlesUtil.PathCombine(_packageDirectory, arr[1]);
                    operation.decompressSize = long.Parse(arr[2]);
                    operation.compressSize = long.Parse(arr[3]);
                    operation.updateMode = (MyAssetBundlesUpdateMode)Enum.Parse(typeof(MyAssetBundlesUpdateMode), arr[4]);
                    _versionsList.Add(operation);
                } catch (Exception)
                {
                    continue;
                }
            }
        }

        public List<MyVersionOperation>  GetListWithVersion (string version)
        {
            List<MyVersionOperation> list = new List<MyVersionOperation>();
            for (int i = 0; i < _versionsList.Count; i++)
            {
                if (_versionsList[i].version == version)
                    list.Add(_versionsList[i]);
            }
            return list;
        }

        public MyVersionOperation GetInfoWithVersion (string version, MyAssetBundlesUpdateMode updateMode)
        {
            for (int i = 0; i < _versionsList.Count; i++)
            {
                if (_versionsList[i].version == version && updateMode == _versionsList[i].updateMode)
                    return _versionsList[i];
            }
            return null;
        }

        public bool AddVersion (MyVersionOperation operation)
        {
            _versionsList.Add(operation);
            Flush();
            return true;
        }

        public bool RemoveVersion (MyVersionOperation operation)
        {
            bool ret = _versionsList.Remove(operation);
            if (ret) Flush();
            return ret;
        }

        protected void Flush ()
        {
            if (_filePath == null) return;
            StreamWriter sw = File.CreateText(_filePath);
            foreach (var item in _versionsList)
            {
                string str = string.Format("{0}#{1}#{2}#{3}#{4}", 
                    item.version, 
                    Path.GetFileName(item.compressPackage), 
                    item.decompressSize, 
                    item.compressSize, 
                    item.updateMode
                );
                sw.WriteLine(str);
            }
            sw.Flush();
            sw.Close();
        }

        public IEnumerator<MyVersionOperation> GetEnumerator ()
        {
            return ((IEnumerable<MyVersionOperation>)_versionsList).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator ()
        {
            return ((IEnumerable<MyVersionOperation>)_versionsList).GetEnumerator();
        }
    }
}
